#include "WorldObjectManager.h"

WorldObjectManager::WorldObjectManager()
{
}

WorldObjectManager::~WorldObjectManager()
{
}

WorldObjectManager* WorldObjectManager::m_xInstance = nullptr;

WorldObjectManager* WorldObjectManager::GetInstance()
{
	if (m_xInstance == nullptr)
	{
		m_xInstance = new WorldObjectManager;
	}
	return m_xInstance;
}

void WorldObjectManager::Destroy()
{
	if (m_xInstance != NULL)
	{
		delete m_xInstance;
		m_xInstance = NULL;
	}
}

void WorldObjectManager::Initialize(std::vector<std::vector<Node*>> &pGrid)
{
	Grid = pGrid;
	m_fSpawnDelay = 0.0f;
	CreateMultipleWorldObjects(10, TypePlant);
}

void WorldObjectManager::Update(float pDeltaTime)
{
	for (unsigned int i = 0; i < m_vWorldObjects.size(); i++)
	{
		if (!m_vWorldObjects[i]->ShouldBeDeleted())
		{
			m_vWorldObjects[i]->Update(pDeltaTime);
		}
		else
		{
			DeleteObject(m_vWorldObjects[i]);
		}
	}

	if (m_fSpawnDelay <= 0.0f)
	{
		CreateMultipleWorldObjects(25, TypePlant);
		m_fSpawnDelay = 30.0f;
	}
	else
	{
		m_fSpawnDelay -= pDeltaTime;
	}
}

void WorldObjectManager::Cleanup()
{
	for (unsigned int i = 0; i < m_vWorldObjects.size(); i++)
	{
		m_vWorldObjects[i]->Cleanup();
		delete m_vWorldObjects[i];
		m_vWorldObjects[i] = nullptr;
	}
	m_vWorldObjects.clear();
}

void WorldObjectManager::AddObject(WorldObject* pObject)
{
	for (int i = 0; i < m_vWorldObjects.size(); i++)
	{
		if (m_vWorldObjects[i] == pObject)
		{
			std::cout << "Error - Object already exists in the vector" << std::endl;
			return;
		}
	}

	m_vWorldObjects.push_back(pObject);
}

void WorldObjectManager::DeleteObject(WorldObject* pObject)
{
	for (unsigned int i = 0; i < m_vWorldObjects.size(); i++)
	{
		if (m_vWorldObjects[i] == pObject)
		{
			m_vWorldObjects[i]->Cleanup();
			delete m_vWorldObjects[i];
			m_vWorldObjects.erase(m_vWorldObjects.begin() + i);
			return;
		}
	}
}

void WorldObjectManager::CreateMultipleWorldObjects(int pAmount, ObjectTypes pType)
{
	
	for (int i = 0; i < pAmount; i++)
	{
		bool foundEmpty = false;
		int RandX = 0;
		int RandY = 0;
		while (!foundEmpty)
		{
			RandX = rand() % 30;
			RandY = rand() % 30;

			if (Grid[RandX][RandY]->m_xWorldObject == nullptr && Grid[RandX][RandY]->m_bBlocked == false)
			{
				foundEmpty = true;
			}
		}

		switch (pType)
		{
			case TypePlant:
				Plant* worldObject = new Plant();
				worldObject->Initialize();

				worldObject->SetCurrentNode(Grid[RandX][RandY]);
				worldObject->SetPosition(Grid[RandX][RandY]->m_vPosition);
				Grid[RandX][RandY]->m_xWorldObject = worldObject;
				AddObject(worldObject);
				break;
		}
	}
}

void WorldObjectManager::CreateWorldObject(int pX, int pY, ObjectTypes pType, bool pReplace)
{
	// Space taken
	if (Grid[pX][pY]->m_bBlocked == true)
	{
		return;
		std::cout << "Blocked" << std::endl;
	}

	if (Grid[pX][pY]->m_xWorldObject != nullptr)
	{
		if (!pReplace)
		{
			std::cout << "Blocked" << std::endl;
			return;
		}
		else
		{
			DeleteObject(Grid[pX][pY]->m_xWorldObject);
			Grid[pX][pY]->m_xWorldObject = nullptr;
		}
	}

	switch (pType)
	{
		case TypePlant:
			Plant* worldObject = new Plant();
			worldObject->Initialize();

			worldObject->SetCurrentNode(Grid[pX][pY]);
			worldObject->SetPosition(Grid[pX][pY]->m_vPosition);
			Grid[pX][pY]->m_xWorldObject = worldObject;
			AddObject(worldObject);
			break;

		//case TypeWater:

		//	break;
	}

	std::cout << "Spawned Object" << std::endl;
}
